﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ModeloGaleriaNET.jUDDIInquiry;
using ModeloGaleriaNET.jUDDISecurity;
using ModeloGaleriaNET.jUDDIPublish;

namespace ModeloGaleriaNET
{
    public class jUDDIManager
    {
        private jUDDIManager()
        { }

        public static readonly jUDDIManager Instance = new jUDDIManager();

        
        public bool UpdateURL()
        {
            int https;
            UDDI_Inquiry_Port inquiry;
            find_service findService = new find_service();
            serviceList listaServicios = null;
            get_serviceDetail gsd;
            jUDDIInquiry.serviceInfo si;
            jUDDIInquiry.serviceDetail detail;
            jUDDIInquiry.name[] nombre = { new jUDDIInquiry.name(), new jUDDIInquiry.name() };
            string[] findQualifiers = {"approximateMatch"};
            string url;
            string[] splittedURL;

            https = Properties.Settings.Default.Seguridad;
            nombre[0].Value ="%Commands%";
            nombre[1].Value = "%Queries%";
            findService.findQualifiers = findQualifiers;
            findService.name = nombre;
            inquiry = new UDDI_Inquiry_Port();
            inquiry.Timeout = (int)Properties.Settings.Default.TimeOut * 1000;
            inquiry.Url = https == 0 ? Properties.Settings.Default.ModeloGaleriaNET_jUDDIInquiry_UDDI_Inquiry_Port 
                : Properties.Settings.Default.ModeloGaleriaNET_jUDDIPublish_UDDI_Publish_Port_SSL;
            try
            {
                listaServicios = inquiry.find_service(findService);
                // De momento, se coge el primer servicio y se extrae la IP (sin puerto)
                gsd = new get_serviceDetail();
                si = listaServicios.serviceInfos[0];
                gsd.serviceKey = new string[] { si.serviceKey };
                detail = inquiry.get_serviceDetail(gsd);
                url = detail.businessService[0].bindingTemplates[0].accessPoint.Value;
                splittedURL = url.Split(new char[] {'/'});

                foreach (string s in splittedURL)
                {
                    if (System.Text.RegularExpressions.Regex.IsMatch(s, @"^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(:[0-9]{1,5})?$"))
                    {
                        url = s.Remove(s.LastIndexOf(':'));
                        ServerConnData.Instance.URLGestor = url;
                        ServerConnData.Instance.Save();
                        break;
                    }
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool GetAuthToken()
        {
            int https;

            https = Properties.Settings.Default.Seguridad;
            UDDI_Security_Port sec = new UDDI_Security_Port();
            get_authToken getAuthToken = new get_authToken();
            getAuthToken.userID = "root";
            getAuthToken.cred = string.Empty;
            sec.Timeout = (int)Properties.Settings.Default.TimeOut * 1000;
            sec.Url = https == 0 ? Properties.Settings.Default.ModeloGaleriaNET_jUDDISecurity_UDDI_Security_Port
                : Properties.Settings.Default.ModeloGaleriaNET_jUDDISecurity_UDDI_Security_Port_SSL;

            try
            {
                sec.get_authToken(getAuthToken);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
